<template>
  <div class="form-container-wrap">
    <el-card class="form-card">
      <template #header>
        <div class="card-header">
          <span>{{ formTitle }}</span>
          <el-button class="back-button" type="primary" @click="goBack">返回列表</el-button>
        </div>
      </template>
      <el-form :model="form" ref="formRef" :rules="rules" label-width="100px" class="form-content">
        <el-form-item label="资源名称" prop="resourceName">
          <el-input v-model="form.resourceName" placeholder="请输入资源名称" />
        </el-form-item>
        <el-form-item label="资源类型" prop="resourceType">
          <el-input v-model="form.resourceType" placeholder="请输入资源类型" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSubmit">
            {{ isEdit ? '更新' : '创建' }}
          </el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage, FormInstance, FormRules } from 'element-plus'
import { resourcesApi } from '@/api'
import { Resources } from '@/types'

const router = useRouter()
const route = useRoute()
const formRef = ref<FormInstance | null>(null)

const isEdit = ref(false)
const formTitle = ref('创建新资源')

// 表单数据
const form = reactive<Resources>({
  resourceName: '',
  resourceType: ''
})

// 表单验证规则
const rules: FormRules = {
  resourceName: [{ required: true, message: '请输入资源名称', trigger: 'blur' }],
  resourceType: [{ required: true, message: '请输入资源类型', trigger: 'blur' }]
}

onMounted(() => {
  const resourceId = route.params.id as string
  if (resourceId) {
    isEdit.value = true
    formTitle.value = '编辑资源'
    fetchResourceDetails(Number(resourceId))
  }
})

const fetchResourceDetails = async (id: number) => {
  try {
    const response = await resourcesApi.getById(id)
    if (response.code === 200) {
      Object.assign(form, response.data)
    } else {
      ElMessage.error(response.message || '获取资源详情失败')
    }
  } catch (error) {
    console.error('获取资源详情失败:', error)
    ElMessage.error('获取资源详情失败')
  }
}

const handleSubmit = () => {
  formRef.value?.validate(async (valid) => {
    if (valid) {
      try {
        let response
        if (isEdit.value) {
          // 修改更新请求参数格式
          response = await resourcesApi.update({
            ...form,
            resourceId: form.resourceId as number
          })
        } else {
          response = await resourcesApi.add(form)
        }
        if (response.code === 200) {
          ElMessage.success(isEdit.value ? '资源更新成功' : '资源创建成功')
          router.push('/system/resources')
        } else {
          ElMessage.error(response.message || '操作失败')
        }
      } catch (error) {
        console.error('操作失败:', error)
        ElMessage.error('操作失败')
      }
    }
  })
}

const handleReset = () => {
  formRef.value?.resetFields()
}

const goBack = () => {
  router.push('/system/resources')
}
</script>

<style scoped>
.form-container-wrap {
  padding: 20px;
}
.form-card {
  max-width: 800px;
  margin: 0 auto;
}
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.form-content {
  padding-top: 20px;
}
</style> 